package main

import (
	"os/signal"
	"server/src/logging"
	"server/src/storage"
	"syscall"

	"os"
)

const VER string = "胎温胎压监控服务器 v1.0 build 220627"

func main() {
	var err error

	initGlobal()

	storage.Init(storage.RootPath())

	// 启动日志
	logging.DisableBootScreenLog()
	logging.LogBoot()

	zLog(nil, VER, " 启动")

	cfg, err := loadConfig("")
	if err != nil {
		zLog(nil, err.Error())
		return
	}

	dbo, err := NewDBOperation(cfg)
	if err != nil {
		zLog(nil, "数据库连接失败，程序退出:", err)
		return
	} else {
		zLog(nil, "数据库连接成功")
	}
	defer dbo.Close()

	alarmCall := NewAlarmcall(&(cfg.AlarmCall), dbo)
	defer alarmCall.Close()

	connManager := NewConnManager()

	NewProcessor(cfg, dbo, connManager, alarmCall).Run()

	Listen(cfg, dbo, connManager)

	signalChan := make(chan os.Signal, 1)
	signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)

	s := <-signalChan
	zLog(nil, VER, "收到退出信号:", s)
}
